package net.sx.minemodule.mvp.activities;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.addimg.ViewImgFragmentActivity01;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.crop.TakePhotoHelper;
import com.kedll.library.listener.OnClickListener;
import com.kedll.library.mvp.BasePresenter;
import com.kedll.library.util.CollectionUtils;
import com.kedll.library.contants.OnItemClickListener;

import net.sx.minemodule.R;
import net.sx.minemodule.adapter.PushScoreAdapter;
import net.sx.minemodule.databinding.MineActivityPushscoreBinding;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * 发表评论
 */
@RuntimePermissions
@Route(path = ARouterPath.PATH_MINE_PUSHSCORE)
public class MinePushScoreActivity extends MyBaseFragmentActivity<MineActivityPushscoreBinding, BasePresenter> implements OnItemClickListener {
    public static final int MAX_COUNT = 4;

    /**
     * 添加图片的Item
     */
    Map<String, Object> addMap = new HashMap<>();
    private TakePhotoHelper photoHelper;
    private PushScoreAdapter adapter;
    private List<String> pics;

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.mine_activity_pushscore;
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("发表评论");

    }

    @Override
    protected void initEvent() {
        adapter.setOnItemClickListener(this);
        photoHelper.setOnTakePhotoCompleteListener(picStr -> {
            adapter.clear();
            adapter.addAll(photoHelper.getImagesDatas());

            if (CollectionUtils.size(photoHelper.getImagesDatas()) < MAX_COUNT) {
                if (!adapter.getValues().contains(addMap)) {
                    adapter.getValues().add(addMap);
                }
            }
        });

        setTopRightButton("发布", v -> {
            //TODO
        });

    }

    /**
     * MsgBody=我认为你认为的可能不对           //消息内容
     * ExtAppSid=comdiy_000                     //商品sid
     * InteractiveSubject=news_1234             //订单商品记录的sid
     * AppType=0                                //固定传0
     * ExtData={"material":"物流评分","server":"商家服务"}
     */
    private void pushScore() {
//        Map<String, Object> map = new HashMap<>();
//        map.put("MsgBody", mBinding.pushScoreEtDescription.getText());
//        map.put("ExtAppSid", );
//        map.put("InteractiveSubject", );
//        map.put("AppType", );
//        Map<String, Object> extData = new HashMap<>();
//        extData.put("material", "");
//        extData.put("server", "");
//        map.put("ExtData", extData);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        photoHelper = new TakePhotoHelper(this);
        photoHelper.setIsCrop(false);
        adapter = new PushScoreAdapter(getApplicationContext());
        pics = new ArrayList<>();

        if (CollectionUtils.size(pics) < MAX_COUNT) {
            addMap.put("isAdd", true);
            adapter.add(addMap);
        }
        mBinding.pushScorePics.setAdapter(adapter);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MinePushScoreActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        photoHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void itemClick(View v, Map<String, Object> item, int position) {
        int i = v.getId();
        if (i == R.id.iv_add_photo) {
            //添加图片
            MinePushScoreActivityPermissionsDispatcher.onTakePhotoWithPermissionCheck(this);

        } else if (i == R.id.iv_del_photo) {
            //删除图片
            photoHelper.removeImage(item);
            adapter.remove(item);
            if (!adapter.getValues().contains(addMap)) {
                adapter.getValues().add(addMap);
            }

        } else if (i == R.id.iv_photo) {
            //查看大图
            Intent intent = new Intent(this, ViewImgFragmentActivity01.class);
            intent.putExtra("selectedList", (Serializable) adapter.getValues());
            intent.putExtra("current", position);
            startActivity(intent);
            overridePendingTransition(enterAnim, exitAnim);

        } else {
        }
    }

    @NeedsPermission({Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onTakePhoto() {
        photoHelper.showPopup(MAX_COUNT);
    }
}
